﻿using Microsoft.VisualStudio.Web.CodeGeneration.Contracts.Messaging;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.X9;
using QRCodeBluetooth.DAL;
using QRCodeBluetooth.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using System.Security.Policy;
using Microsoft.CodeAnalysis;

namespace QRCodeBluetooth.BUS
{
    public class RecordBUS
    {
        public ResponseMessage QueryRecord(RecordInfo recordInfo)
        {
            ResponseMessage message = new ResponseMessage();
            RecordDAL da = new RecordDAL();
            message.errcode = 0;
            message.data = da.QueryRecord(recordInfo);
            message.total = da.QueryRecordCount(recordInfo);

            return message;
        }

        public ResponseMessage DeleteRecord(string ids)
        {
            ResponseMessage message = new ResponseMessage();
            RecordDAL da = new RecordDAL();

            bool result = da.DeleteRecord(ids);
            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "删除审核信息失败";
            }
            return message;
        }

        public ResponseMessage AddRecord(AppAuditRecord appAuditRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordDAL recordDAL = new RecordDAL();

            int auditnum = recordDAL.QueryExistAudit(appAuditRecord.identityInf, appAuditRecord.projectId);
            if (auditnum > 0)
            {
                appAuditRecord.id = auditnum;
                bool result = recordDAL.UpdateRecord(appAuditRecord);
                if (result)
                {
                    message.errcode = 0;
                    //bool sync_result = PushAuditInfo(appAuditRecord, auditnum);
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "提交审核信息失败";
                }
            }
            else
            {
                int result = recordDAL.AddRecord(appAuditRecord);
                if (result > 0)
                {
                    message.errcode = 0;
                    bool sync_result = PushAuditInfo(appAuditRecord, result);
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "提交审核信息失败";
                }
            }


            return message;
        }

        public bool PushAuditInfo(AppAuditRecord appAuditRecord, int auditid)
        {
            try
            {
                ProjectDAL projectDAL = new ProjectDAL();
                Models.ProjectInfo projectInfo = projectDAL.LoadProjectInfo(appAuditRecord.projectId);
                AddAuditInfo addAuditInfo = new AddAuditInfo();
                addAuditInfo.method = "addaudit";
                addAuditInfo.ProjectKey = projectInfo.project_key;
                QRCodeAuditInfoData qRCodeAuditInfoData = new QRCodeAuditInfoData();
                qRCodeAuditInfoData.AuditID = auditid;
                qRCodeAuditInfoData.ApplicationTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                qRCodeAuditInfoData.OwnerAddress = appAuditRecord.ownerAddress;
                qRCodeAuditInfoData.OwnerName = appAuditRecord.ownerName;
                qRCodeAuditInfoData.Phone = appAuditRecord.phone;
                addAuditInfo.data = qRCodeAuditInfoData;
                string re = HttpClientPost(projectInfo.project_ip + "/api/Request/AddAudit", addAuditInfo);
                AddAuditInfoReturn qRCodeReturn = JsonConvert.DeserializeObject<AddAuditInfoReturn>(re);
                if (qRCodeReturn != null && qRCodeReturn.code == 0 && qRCodeReturn.method == "addaudit")
                {
                    RecordDAL recordDAL = new RecordDAL();
                    recordDAL.UpdateSyncFlag(qRCodeReturn.data.AuditID);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }
    }
}
